package core
{
	import events.Msg_FileDownload;
	import events.Msg_FileDownloading;
	import events.Msg_GetVersionInfo;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	
	import interfaces.IMsgListener;

	public class DownloadCenter implements IMsgListener
	{
		private static const MAX_DOWNLOAD:int = 4;
	
		private var m_version:Dictionary;				// 版本索引表
		private var m_tasks:Array;					// 下载任务表
		private var m_current:int;					// 当前正在下载的任务数
		private var m_loaders:Vector.<Loader> ;		// 加载器
		
		public function DownloadCenter()
		{
			m_version = new Dictionary();
			m_tasks = new Array();
			Global.addListener( Msg_GetVersionInfo.M_GETVERSIONINFO,this);
		}
		
		public function msgManager(e:* = null):void
		{
			
		}
		
		public function loadSwf(url:String):void
		{
			var task:Object = new Object;
			task.url = url; 
			task.loader = new Loader();
			m_tasks.push(task);
			startDownload();
		}
		
		public function loadSwfs(urls:Vector.<String>):void
		{
			
		}
		
		
		protected function startDownload():void
		{
			if (m_tasks.length == 0 || m_current >= MAX_DOWNLOAD)
				return;
			
			var task:Object = m_tasks[0];
			var ldr:Loader = task.loader;
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onDownloadOK);
			ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onDownloadError);
			ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			ldr.load(new URLRequest(task.url));
			
			m_current++;
			startDownload(); // 继续调用，以便下载队列始终饱满
		}
		
		protected function onProgress(e:ProgressEvent):void 
		{
			var ldr:Loader = Loader(LoaderInfo(e.target).loader);
			for(var i:int = 0; i < m_tasks.length; i++)
			{
				if(m_tasks[i].loader == ldr)break;
			}
			Global.dispatchEvent(Msg_FileDownloading.M_FILEDOWNLOADING,
			Msg_FileDownloading.createMessage(m_tasks[i].url,int(e.bytesLoaded * 100 / e.bytesTotal)));
		}
		
		protected function onDownloadOver(ldr:Loader,suc:Boolean):void 
		{
			// 删除下载监听器
			for(var i:int = 0; i < m_tasks.length; i++)
			{
				if(m_tasks[i].loader == ldr)break;
			}
			ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, onDownloadOK);
			ldr.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onDownloadError);
			ldr.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
		
			Global.dispatchEvent( Msg_FileDownload.M_FILEDOWNLOAD,
				Msg_FileDownload.createMessage(m_tasks[i].url,suc,ldr));
			m_tasks.shift();
			// 启动队列中等待的下载申请
			m_current--;
			startDownload();
		}
		
		protected function onDownloadOK(e:Event):void 
		{ 
			onDownloadOver(Loader(LoaderInfo(e.target).loader),true);  
		}
		protected function onDownloadError(e:Event):void 	{ onDownloadOver(Loader(LoaderInfo(e.target).loader),false); }
		
	}
}